package com.wuchao.mapreduce.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @author 吴超
 * @time 2021/10/6 17:09
 * KEYIN, reduce阶段，输入的key的类型：Text
 * VALUEIN, reduce阶段，输入value的类型：IntWritable
 * KEYOUT, reduce阶段，输出key的类型：Text
 * VALUEOUT, reduce阶段，输出value的类型：IntWritable
 */
public class WordCountReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
    private IntWritable outV = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        // 1.遍历values，这个可迭代的集合
        int sum = 0;
        for (IntWritable value :
                values) {
            sum += value.get(); // 类型转换
        }
        // 2.封装
        outV.set(sum);
        // 3.写出kv
        context.write(key,outV);
    }
}
